package com.cloudwise.controller;

import com.cloudwise.entity.Dept;
import com.cloudwise.entity.LayUiTree;
import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.User;
import com.cloudwise.service.DeptService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2020-12-17 10:13:24
 */
@RestController
@RequestMapping("dept")
public class DeptController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;


    /**
     * @return
     * @create by: Teacher陈
     * @description: 跳转到显示所有部门页面
     * @create time: 2020/12/22 10:07
     */
    @GetMapping("toShowDept")
    public ModelAndView toShowDept(ModelAndView modelAndView) {
        modelAndView.setViewName("dept/showDept");
        return modelAndView;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Dept selectOne(Integer id) {
        return this.deptService.queryById(id);
    }


    @RequestMapping("/selectAllDept")
    @ResponseBody
    public ReturnBean selectAllDept(Dept dept) {

        List<Dept> userList = deptService.queryAll(dept);
        //返回json结果
        ReturnBean returnBean = returnSuccess(userList);
        returnBean.setCount((long) userList.size());
        return returnBean;
    }

    /**
     * @return List<LayUiTree>
     * @create by: Teacher陈
     * @description: 查询所有部门，显示部门树
     * @create time: 2020/12/22 16:37
     */
    @RequestMapping("/findAllDeptTree")
    @ResponseBody
    public List<LayUiTree> findAllDeptTree() {
        List<LayUiTree> layUiTreeList = deptService.findDepts(null);
        return layUiTreeList;
    }

    /**
     * 保存部门
     *
     * @param dept
     * @return
     */
    @RequestMapping("/saveDept")
    @ResponseBody
    public ReturnBean saveDept(Dept dept) {
        try {
            //创建人，创建时间
            dept = setCreate(dept);
            dept = deptService.insert(dept);
            return returnSuccess(dept);
        } catch (Exception e) {
            return returnFail(null);
        }
    }

    /**
     * 更新部门信息（controller中的方法作用1：页面跳转，2：收受参数，返回参数
     * 不建议在controller中加入过多的业务逻辑）
     *
     * @param dept
     * @return
     */
    @RequestMapping("/updateDept")
    @ResponseBody
    public ReturnBean updateDept(Dept dept) {
        //修改人，修改时间
        if (null == dept.getStatus()) {
            dept.setStatus("1");
        }
        setUpdate(dept);
        try {
            deptService.update(dept);
            return returnSuccess(null);
        } catch (Exception e) {
            return returnFail(null);
        }
    }

    @RequestMapping("/deleteDept")
    @ResponseBody
    public ReturnBean deleteDept(Dept dept) {
        try {
            deptService.deleteById(dept.getDeptId());
            return returnSuccess(null);
        } catch (Exception e) {
            return returnFail(null);
        }
    }

    @RequestMapping(value = "/deleteBatchDept")
    @ResponseBody
    public ReturnBean deleteBatchDept(@RequestBody List<Dept> deptList) {

        boolean update = deptService.updateBatchById(deptList);
        if (update) {
            return returnSuccess(null);
        } else {
            return returnFail(null);
        }
    }

    @RequestMapping("/findParentNameId")
    @ResponseBody
    public ReturnBean findParentNameId(Integer deptId) {
        Dept dept = deptService.queryById(deptId);
        if (null == dept) {
            //menuId为0的时候，返回的对象是空，防止返回前台为空
             dept = new Dept();
        }
        return super.returnSuccess(dept);
    }

}